package com.ccp.dev.form.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.form.dao.SysHistoryDataDao;
import com.ccp.dev.form.model.SysHistoryData;
import com.ccp.dev.system.model.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 历史记录 service类
 *
 * @author zhaowj
 */
@Service
public class SysHistoryDataService extends BaseService<SysHistoryData> {

    @Resource
    SysHistoryDataDao sysHistoryDataDao;

    /**
     * 添加历史数据。
     *
     * @param dataType
     * @param content
     * @param relateId
     */
    @Transactional(rollbackFor = Exception.class)
    public void addSysHistoryData(String dataType, String subject, String content, String relateId) {
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();

        SysHistoryData data = new SysHistoryData();
        data.setId(UUIDUtils.getUUIDFor32());
        data.setContent(content);
        data.setSubject(subject);
        data.setType(dataType);
        data.setObjId(relateId);
        data.setCreatetime(new Date());
        if (BeanUtils.isNotEmpty(sysUser)) {
            data.setCreator(sysUser.getFullName());
        }

        sysHistoryDataDao.add(data);
    }

}
